import axios from "axios";
import { message } from "ant-design-vue";

// 由于可能用到多个代理,统一封装拦截函数
const interceptors = (req) => {
  req.interceptors.request.use((config) => {
    return config
  })
  req.interceptors.response.use((response) => {
    return response.data
  }, (error) => {
    console.log(error.response.status);
    let status = error.response.status
    switch (status) {
      case 404:
        message.error('请求路径有误')
        break
      case 401:
        message.error('请求参数有误')
        break
      case 500 | 501 | 502 | 503 | 504 | 505:
        message.error('服务器异常')
        break
      default:
        return Promise.reject(new Error(error.message))
    }
  })
  return req
}

// 定义请求
let request = axios.create({
  baseURL: '/api',
  timeout: 5000
})

// 添加拦截器
request = interceptors(request)

export { request }